﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Arizu.Repository;
using Arizu.Entity;
using System.Data;

namespace AiRiZu
{
    public partial class Room : System.Web.UI.Page
    {
        int year;//年
        int month;//月
        int weekstart;
        int daycount;//月天数
        int pdaycount;
        int ldaycount;
        public string adrdesc = "";
        public string str = "";
        Arizu.Entity.SendRoom list = new Arizu.Entity.SendRoom();
        public string img0 = "";
        public string arr = "";
        public string imgstr = "";
        public string name = "";
        public int fangdongroomcount = 0;
        public string fangdongroominfo = "";
        public string pinglunstr = "";
        public string moneyvalue = "";
        public int RoomId = -1;

        public int DayPrice { get; set; }

        public string IsTejiaHtml = "";
        //Arizu.Entity.ImgSendRoom imglist = new Arizu.Entity.ImgSendRoom();
        protected void Page_Load(object sender, EventArgs e)
        {
            //row5.Visible = true;
            //row6.Visible = true;
            //for (int i = 0; i < 42; i++)
            //{
            //    Label lb = (Label)Page.FindControl("LB" + i);
            //    Label lbb = (Label)Page.FindControl("LBB" + i);
            //    lb.Text = "";
            //    lbb.Text = "";
            //}
            if (!IsPostBack)
            {
                InitPage();
                //year = DateTime.Now.Year;
                //month = DateTime.Now.Month;
                //show(year, month);
            }
            GetRoomID();
            GetDayPrice();
        }

        private void GetRoomID()
        {
            if (Request.QueryString["sendroomid"] != null && Request.QueryString["sendroomid"] != "")
            {
                RoomId = int.Parse(Request.QueryString["sendroomid"]);
            }
        }

        private void GetDayPrice()
        {
            if (Request.QueryString["sendroomid"] != null && Request.QueryString["sendroomid"] != "")
            {
                //int sendroomid = int.Parse(Request.QueryString["sendroomid"]);

                AirizuPrice airizuPrice = AirizuPrice.CreateInstance();
                DataTable dt = airizuPrice.GetPrice(RoomId);

                if (dt.Rows.Count > 0)
                {
                    DayPrice = int.Parse(dt.Rows[0]["DayPrice"].ToString());

                    if (dt.Rows[0]["CheckIn"] != null && dt.Rows[0]["CheckOut"] != null && !string.IsNullOrEmpty(dt.Rows[0]["CheckIn"].ToString()) && !string.IsNullOrEmpty(dt.Rows[0]["CheckOut"].ToString()))
                    {
                        long checkin = long.Parse(dt.Rows[0]["CheckIn"].ToString());
                        long checkout = long.Parse(dt.Rows[0]["CheckOut"].ToString());

                        if (System.DateTime.Now.Ticks > checkin && System.DateTime.Now.Ticks < checkout)
                        {
                            IsTejiaHtml = "<img src=\"img/tejia.jpg\" />";
                        }
                        else
                        {
                            IsTejiaHtml = "";
                        }
                    }
                }
            }
        }

        //private void show(int Nyear, int Nmonth)
        //{
        //    int seat = 0;
        //    DateTime Nymd = new DateTime(Nyear, Nmonth, 1);
        //    weekstart = (int)Nymd.DayOfWeek;
        //    daycount = DateTime.DaysInMonth(Nyear, Nmonth);
        //    //monthlb.Text = month.ToString();
        //    //yearlb.Text = year.ToString();
        //    for (int i = weekstart, j = 1; i < weekstart + daycount; i++, j++)
        //    {
        //        Label lb = (Label)Page.FindControl("LB" + i);
        //        Label lbb = (Label)Page.FindControl("LBB" + i);
        //        lb.Text = j.ToString();
        //        lbb.Text = (Nmonth == 1 && j == 1) ? "元旦" : (Nmonth == 5 && j == 1) ? "劳动节" : (Nmonth == 10 && j == 1) ? "国庆节" : "";
        //        seat = i;
        //    }
        //    DateTime Pymd = Nymd.AddMonths(-1);
        //    DateTime Lymd = Nymd.AddMonths(1);
        //    pdaycount = DateTime.DaysInMonth(Pymd.Year, Pymd.Month);
        //    ldaycount = DateTime.DaysInMonth(Lymd.Year, Lymd.Month);
        //    #region  //补齐
        //    if (weekstart > 0)
        //    {
        //        for (int i = weekstart - 1; i >= 0; i--, pdaycount--)
        //        {
        //            Label lb = (Label)Page.FindControl("LB" + i);
        //            lb.Text = pdaycount.ToString();
        //        }
        //    }
        //    //if (seat == 27)
        //    //{
        //    //    row5.Visible = false;
        //    //    row6.Visible = false;
        //    //}
        //    //else if (seat < 35)
        //    //{
        //    //    for (int i = seat + 1, j = 1; i <= 35; i++, j++)
        //    //    {
        //    //        Label lb = (Label)Page.FindControl("LB" + i);
        //    //        lb.Text = j.ToString();
        //    //    }
        //    //    row6.Visible = false;
        //    //}
        //    //else if (seat < 41)
        //    //{
        //    //    for (int i = seat + 1, j = 1; i <= 41; i++, j++)
        //    //    {
        //    //        Label lb = (Label)Page.FindControl("LB" + i);
        //    //        lb.Text = j.ToString();
        //    //    }
        //    //}
        //    #endregion
        //}

        private void InitPage()
        {
            if (Request.QueryString["sendroomid"] != null && Request.QueryString["sendroomid"] != "")
            {
                int sendroomid = int.Parse(Request.QueryString["sendroomid"]);

                //初始化，加载数据
                try
                {
                    //房间图片
                    DataTable dt = Main.CreateInstance().GetImgList(sendroomid);
                    if (dt.Rows.Count > 0)
                    {
                        for (int i = 0; i < 26; i++)
                        {
                            if (dt.Rows[0]["Image" + i] != null && dt.Rows[0]["Image" + i].ToString() != "")
                            {
                                img0 = dt.Rows[0]["Image0"].ToString();
                                arr += dt.Rows[0]["Image" + i].ToString() + ",";
                                str += "<li><a href=\"#\" onclick=\"getimg(this.id);\" id=\"img" + i + "\"><img width=\"64\" height=\"48\" src=\"smallpic/" + dt.Rows[0]["Image" + i].ToString() + "\" /></a></li>";
                            }
                        }
                        arr = arr.TrimEnd(',');
                    }
                    //房东信息
                    DataTable mydt = Main.CreateInstance().Getpeopleinfo(sendroomid.ToString());
                    if (mydt.Rows.Count > 0)
                    {
                        if (mydt.Rows[0]["Image"].ToString() != "" && mydt.Rows[0]["Image"] != null)
                        {
                            imgstr = "Userpic/" + mydt.Rows[0]["Image"].ToString();
                        }
                        else
                        {
                            imgstr = "Userpic/default_user_pic.png";
                        }
                        name = mydt.Rows[0]["Xing"].ToString() + mydt.Rows[0]["Ming"].ToString();
                    }
                    //房东发布的其他房间信息
                    DataTable fangdongdt = Main.CreateInstance().Getfangdongsendroominfo(sendroomid.ToString());
                    if (fangdongdt.Rows.Count > 0)
                    {
                        fangdongroominfo += "<ul>";
                        for (int i = 0; i < fangdongdt.Rows.Count; i++)
                        {
                            fangdongroominfo += "<li><em><a href=\"Room.aspx?sendroomid=" + fangdongdt.Rows[i]["SendRoomid"].ToString() + "\">";
                            fangdongroominfo += "<img src=\"imgs/" + fangdongdt.Rows[i]["Image0"].ToString() + "\" title=\"" + fangdongdt.Rows[i]["RoomTitle"].ToString() + "\" /></a></em><span><a href=\"Room.aspx?sendroomid=" + fangdongdt.Rows[i]["SendRoomid"].ToString() + "\">" + fangdongdt.Rows[i]["RoomTitle"].ToString() + "</a></span> <span>￥" + fangdongdt.Rows[i]["JiaGe"].ToString() + "</span>";
                            fangdongroominfo += "</li>";
                            if ((i + 1) % 3 != 0)
                            {
                                fangdongroominfo += "</ul><ul>";
                            }
                        }
                        if (fangdongdt.Rows.Count % 3 == 0)
                        {
                            fangdongroominfo = fangdongroominfo.Substring(0, fangdongroominfo.Length - 4);
                        }
                        else
                        {
                            fangdongroominfo += "</ul>";
                        }
                        fangdongroomcount = fangdongdt.Rows.Count;
                    }
                    this.roomcount.Text = fangdongroomcount.ToString();
                    list = Main.CreateInstance().GetMember(sendroomid);
                    //绑定评论repeater
                    DataTable repdt = Main.CreateInstance().Getroompinglun(sendroomid.ToString());
                    if (repdt.Rows.Count > 0)
                    {
                        this.pinglun.DataSource = repdt;
                        this.pinglun.DataBind();
                    }
                    //房间标题
                    if (list.RoomTitle != "" && list.RoomTitle != null)
                    {
                        this.title.Text = list.RoomTitle;
                    }
                    //房间地址
                    if (list.Adressdesc != "" && list.Adressdesc != null)
                    {
                        this.adressdesc.Text = list.Adressdesc;
                        adrdesc = list.Adressdesc;
                    }
                    //房屋类型
                    if (list.RoomType != "" && list.RoomType != null)
                    {
                        this.roomtype.Text = list.RoomType;
                    }
                    //房间类型
                    if (list.HousesType != "" && list.HousesType != null)
                    {
                        this.fangjiantype.Text = list.HousesType;
                    }
                    //可住人数
                    if (list.PersonNum.ToString() != null && list.PersonNum.ToString() != "")
                    {
                        this.personnum.Text = list.PersonNum.ToString();
                        this.peoplenum.Text = list.PersonNum.ToString();
                    }
                    //卧室数
                    if (list.BedroomNum.ToString() != null && list.BedroomNum.ToString() != "")
                    {
                        this.bedroomnum.Text = list.BedroomNum.ToString();
                    }
                    //床型
                    if (list.Bedtype != "" && list.Bedtype != null)
                    {
                        this.bedtype.Text = list.Bedtype;
                    }
                    //卫生间数
                    if (list.Bathroom.ToString() != null && list.Bathroom.ToString() != "")
                    {
                        this.bathroomnum.Text = list.Bathroom.ToString();
                    }
                    //最少天数
                    if (list.Leastdays.ToString() != null && list.Leastdays.ToString() != "")
                    {
                        this.leastdays.Text = list.Leastdays.ToString();
                        this.leastdaysnum.Text = list.Leastdays.ToString();
                    }
                    //最多天数
                    if (list.Mostdays != "" && list.Mostdays != null)
                    {
                        this.mostdays.Text = list.Mostdays;
                        this.mostdaysnum.Text = list.Mostdays;
                    }
                    //入住时间
                    if (list.Checktime != "" && list.Checktime != null)
                    {
                        this.checktime.Text = list.Checktime;
                    }
                    //退房时间
                    if (list.Checkouttime != "" && list.Checkouttime != null)
                    {
                        this.checkouttime.Text = list.Checkouttime;
                    }
                    //面积
                    if (list.Mianji != "" && list.Mianji != null)
                    {
                        this.mianji.Text = list.Mianji;
                    }
                    //价格
                    if (list.JiaGe != "" && list.JiaGe != null)
                    {
                        moneyvalue = list.JiaGe;
                    }
                }
                catch (Exception)
                {

                    throw;
                }
            }
        }

        public string GetDate(string datet)
        { 
            string[] str=datet.Split(' ');
            string mystr = str[0];
            return mystr;
        }

        //protected void cutMonth(object sender, EventArgs e)
        //{
        //    if (monthlb.Text == "1")
        //    {
        //        month = 12;
        //        year = int.Parse(yearlb.Text) - 1;
        //    }
        //    else
        //    {
        //        month = int.Parse(monthlb.Text) - 1;
        //        year = int.Parse(yearlb.Text);
        //    }
        //    show(year, month);
        //}
        //protected void addMonth(object sender, EventArgs e)
        //{
        //    if (monthlb.Text == "12")
        //    {
        //        month = 1;
        //        year = int.Parse(yearlb.Text) + 1;
        //    }
        //    else
        //    {
        //        month = int.Parse(monthlb.Text) + 1;
        //        year = int.Parse(yearlb.Text);
        //    }
        //    show(year, month);
        //}
        //protected void cutYear(object sender, EventArgs e)
        //{
        //    month = int.Parse(monthlb.Text);
        //    year = int.Parse(yearlb.Text) - 1;
        //    show(year, month);
        //}
        //protected void addYear(object sender, EventArgs e)
        //{
        //    month = int.Parse(monthlb.Text);
        //    year = int.Parse(yearlb.Text) + 1;
        //    show(year, month);
        //}

        protected void contact_Click(object sender, EventArgs e)
        {
            int sendroomid = int.Parse(Request.QueryString["sendroomid"]);
            Session["roomId"] = sendroomid;
            Response.Redirect("user/Contact.aspx");
        }

        protected void confirm_Click(object sender, EventArgs e)
        {
            string sendroomid = Request.QueryString["sendroomid"].ToString();
            string checkIn = Request.Params["ruzhudatevalue"].ToString();
            string checkOut = Request.Params["ruzhudate1value"].ToString();
            string ruzhupeoplenum = Request.Params["ruzhupeoplenum"].ToString();
            Session["roomId"] = sendroomid;
            Session["checkIn"] = checkIn;
            Session["checkOut"] = checkOut;
            Session["ruzhupeoplenum"] = ruzhupeoplenum;
            Response.Redirect("user/Confirm.aspx");
        }
    }
}
